﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace XLDL.Includes
{
    public partial class SanPham : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
                if (!IsPostBack)
                {

                    Session["size"] = "";
                    Session["id"] = "";
                    xuat_luoi();
                    if (Session["sx"].ToString() !="")
                    {

                        for (int i = 0; i < DropDownList_sapxep.Items.Count; i++)
                        {
                            if (DropDownList_sapxep.Items[i].Value.ToString() == Session["sx"].ToString())
                            {
                                DropDownList_sapxep.SelectedIndex = i;
                            }
                        }
                    }
                    
                                  
            }
   
           
            
        }
        protected void xuat_luoi()
        {
        //    try
         //   {
             
                string NID = Request.QueryString["nid"].ToString();
              

                DataTable tb = new DataTable();
            // key sap xep
                if (Session["KeySort"].ToString() == "")
                {
                    Session["KeySort"] = " ngay_dang_sp DESC";
                }

                if (NID == "sp")
                {
                    tb = XLDL.XLDaTa("select * from V_SanPham where id_gt=1 order by " + Session["KeySort"].ToString() + "");//ngay_dang_sp desc");
                    lbl_ten.Text = "Sản phẩm Nữ (" + tb.Rows.Count.ToString() + ")";
                }
                else
            if (NID == "tk")
                {
                    string KY = Request.QueryString["ky"].ToString();
                    string TK = Request.QueryString["tk"].ToString();
                    string lenh = "select * from V_SanPham where ";
                    if (KY == "ms")
                    {
                        if (TK.Length > 2)
                        {
                            string tk = TK.Substring(0, 2);
                            if (tk.ToLower() == "ms")
                            {
                                TK = TK.Remove(0, 2);
                            }
                        }
                        try
                        {
                            

                            lenh += "id_sp =" + int.Parse(TK) + "";
                        }
                        catch
                        {
                            lbl_ten.Text = "Không tìm thấy !!!";
                            CollectionPager1.Visible = false;
                            Panel_sapxep.Visible = false;
                            return;
                        }
                        
                    }
                    else if (KY == "ten")
                    {
                        lenh += "ten_sp like N'%" + TK + "%'";
                    }
                    else if (KY == "gia")
                    {
                        lenh += "gia_sp like '%" + TK + "%'";
                    }
                    else if (KY == "tinhtrang")
                    {
                        if (TK.ToLower() == "còn hàng" || TK.ToLower() == "cònhàng" || TK.ToLower() == "con hang" || TK.ToLower() == "conhang")
                        {
                            lenh += "tinh_trang=1";
                        }
                        else if (TK.ToLower() == "hết hàng" || TK.ToLower() == "hếthàng" || TK.ToLower() == "het hang" || TK.ToLower() == "hethang")
                        {
                            lenh += "tinh_trang=0";
                        }
                        else
                        {
                            lbl_ten.Text = "Không tìm thấy !!!";
                            CollectionPager1.Visible = false;
                            Panel_sapxep.Visible = false;
                            return;
                        }
                    }
                    else 
                    {
                        lenh = "";
                        lenh = "select * from V_Size where so_size='" + TK + "'";
                    }
                    lenh += "order by "+Session["KeySort"].ToString(); //"  order by ngay_dang_sp desc";
                    tb = XLDL.XLDaTa(lenh);
                    lbl_ten.Text = "Kết quả tìm kiếm ("+tb.Rows.Count+")";
                    
                }
                else
                {
                    string id_gt = Request.QueryString["gt"].ToString();
                    tb = XLDL.XLDaTa("select * from V_SanPham where id_nhom_sp=" + int.Parse(NID) + " and id_gt=" + id_gt + " order by " + Session["KeySort"].ToString() + ""); //ngay_dang_sp desc");
                    if (tb.Rows.Count != 0)
                    {
                        lbl_ten.Text = tb.Rows[0]["ten_nhom_sp"].ToString()+" ("+tb.Rows.Count.ToString()+")";
                    }
                }
                
                CollectionPager1.PageSize = 9;
                CollectionPager1.DataSource = tb.DefaultView;
                CollectionPager1.BindToControl = DataList_load;
                DataList_load.DataSource = CollectionPager1.DataSourcePaged;
                if (tb.Rows.Count == 0)
                {
                    lbl_ten.Text = "Không tìm thấy !!!";
                    CollectionPager1.Visible = false;
                    Panel_sapxep.Visible = false;
                }
                DataList_load.DataBind();
                GioHang gh = new GioHang();
                this.PreRender += new EventHandler(gh.GioHang_PreRender);

              
        //    }
      //      catch { }

        }

        protected void DataList_load_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            
            Image img = new Image();
            img = (Image)e.Item.FindControl("Img1");
            Label lb = new Label();
            lb = (Label)e.Item.FindControl("lbl_moi_cu");
            if (lb.Text == "False")
            {
                img.Visible = false;
            }
            // Size
            Label id = new Label();
            id = (Label)e.Item.FindControl("lbl_ID_SP"); // id_SP
            //list Size
            DropDownList drp = new DropDownList();
            drp = (DropDownList)e.Item.FindControl("drp_so_size");
            DataTable tb = new DataTable();
            tb = XLDL.XLDaTa("select * from V_Size where id_sp=" + int.Parse(id.Text) + " order by so_size asc");
            drp.DataSource = tb;
            drp.DataTextField = "so_size";
            drp.DataValueField = "id_size";
            //      Session["size"] = tb;
            drp.DataBind();

            // tinh trang

            Label tt = new Label();
            tt = (Label)e.Item.FindControl("lbl_tinh_trang");
            //enable and disable nut mua ngay
            ImageButton ib = new ImageButton();
            ib = (ImageButton)e.Item.FindControl("b_mua_ngay");
            if (tt.Text == "0") // 0 ko, 1 ko , 2 sap ve 
            {
                tt.Text = " Hết hàng";
                ib.Visible = false;
            }
            else if (tt.Text == "1")
            {
                tt.Text = " Còn hàng";
                ib.Visible = true;
            }
            else
            {
                tt.Text = " Sắp về";
                ib.Visible = true;
            }
            if (Session["size"].ToString() != "")
            {
                for (int i = 0; i <= drp.Items.Count - 1; i++)
                {
                    if (Session["size"].ToString() == drp.Items[i].ToString() && id.Text == Session["id"].ToString())
                    {
                        drp.SelectedIndex = i;
                    }
                }
            }

        }
        protected void DataList_load_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "mua_ngay")
            {

                XL_Gio_hang bang_gio_hang = (XL_Gio_hang)Session["gio_hang"];
                int ms = int.Parse(e.CommandArgument.ToString());
                Label gia_sp = (Label)e.Item.FindControl("lbl_Gia_SP");
                HyperLink ten_sp = (HyperLink)e.Item.FindControl("HyperLink_tensp");
                Label idnhom = (Label)e.Item.FindControl("lbl_id_nhom");
                DropDownList so_size = (DropDownList)e.Item.FindControl("drp_so_size");
                //  DataRow dong = new DataRow();
                int d = bang_gio_hang.Rows.Count;
                bool ok = false;

                DataTable tb = new DataTable();
                tb = XLDL.XLDaTa("select * from V_SanPham where id_sp=" + ms + "");

                if (d == 0)
                {
                    DataRow dong_moi = bang_gio_hang.NewRow();
                    dong_moi["STT"] = 1;
                    dong_moi["Id_SP"] = ms;
                    dong_moi["Id_nhom_SP"] = int.Parse(idnhom.Text);
                    dong_moi["ten_SP"] = ten_sp.Text;
                    dong_moi["so_luong"] = 1;
                    dong_moi["gia_sp"] = double.Parse(gia_sp.Text);
                    dong_moi["so_size"] = so_size.SelectedItem.ToString();
                    dong_moi["gioi_tinh_sp"] = tb.Rows[0]["gioi_tinh_sp"].ToString();
                    dong_moi["id_gt"] = tb.Rows[0]["id_gt"].ToString();
                    dong_moi["hinh_anh"] = tb.Rows[0]["hinh_anh"].ToString();
                    bang_gio_hang.Rows.Add(dong_moi);
                }
                else
                {

                    for (int i = 0; i <= bang_gio_hang.Rows.Count - 1; i++)
                    {
                        if (bang_gio_hang.Rows[i]["so_size"].ToString() == so_size.SelectedItem.ToString() && bang_gio_hang.Rows[i]["id_sp"].ToString() == ms.ToString())
                        {
                            int sl = (int)bang_gio_hang.Rows[i]["so_luong"];
                            bang_gio_hang.Rows[i]["so_luong"] = sl + 1;
                            ok = true;
                            break;
                        }
                    }
                    if (ok != true)
                    {
                        DataRow dong_moi = bang_gio_hang.NewRow();
                        dong_moi["STT"] = 1;
                        dong_moi["Id_SP"] = ms;
                        dong_moi["Id_nhom_SP"] = int.Parse(idnhom.Text);
                        dong_moi["ten_SP"] = ten_sp.Text;
                        dong_moi["so_luong"] = 1;
                        dong_moi["gia_sp"] = double.Parse(gia_sp.Text);
                        dong_moi["so_size"] = so_size.SelectedItem.ToString();
                        dong_moi["gioi_tinh_sp"] = tb.Rows[0]["gioi_tinh_sp"].ToString();
                        dong_moi["id_gt"] = tb.Rows[0]["id_gt"].ToString();
                        dong_moi["hinh_anh"] = tb.Rows[0]["hinh_anh"].ToString();
                        bang_gio_hang.Rows.Add(dong_moi);
                    }

                    int stt = 0;
                    for (int i = 0; i <= bang_gio_hang.Rows.Count - 1; i++)
                    {
                        stt += 1;
                        bang_gio_hang.Rows[i]["STT"] = stt;
                    }
                    // cap nhat sTT

                }


                Session["size"] = so_size.SelectedItem.ToString();
                Session["id"] = ms.ToString();
                // cap nhat tat ca vao bang
                bang_gio_hang.AcceptChanges();
                Session["gio_hang"] = bang_gio_hang;

                // dem so lan xem



            }
            xuat_luoi();
        }

        protected void DropDownList_sapxep_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList_sapxep.SelectedValue.ToString() == "2")
            {
                Session["KeySort"] = " gia_sp desc";
            }
            else if (DropDownList_sapxep.SelectedValue.ToString() == "3")
            {
                Session["KeySort"] = " gia_sp asc";
            }
            else
            {
                Session["KeySort"] = " ngay_dang_sp DESC";
            }
            Session["sx"] = DropDownList_sapxep.SelectedValue.ToString();
            xuat_luoi();
        }

        //protected void Sap_xep(string value)
        //{
        //    DataTable tb = new DataTable();
        //    //  Response.Redirect(Request.Url.ToString() + "&or=" + DropDownList_sapxep.SelectedValue.ToString());
        //    if (value == "2")
        //    {
        //        tb = XLDL.XLDaTa("select * from V_SanPham where id_gt=1 order by gia_sp desc");
        //    }
        //    else if (value== "3")
        //    {
        //        tb = XLDL.XLDaTa("select * from V_SanPham where id_gt=1 order by gia_sp asc");
        //    }
        //    else
        //    {
        //        tb = XLDL.XLDaTa("select * from V_SanPham where id_gt=1 order by ngay_dang_sp desc");
        //    }
        //    lbl_ten.Text = "Sản phẩm Nữ (" + tb.Rows.Count.ToString() + ")";
        //    CollectionPager1.PageSize = 9;
        //    CollectionPager1.DataSource = tb.DefaultView;
        //    CollectionPager1.BindToControl = DataList_load;
        //    DataList_load.DataSource = CollectionPager1.DataSourcePaged;
        //    DataList_load.DataBind();
        //}

    }
}